/**
 * TorrentJEDI
 * 		Torrent Java Episode Download Interface
 */
package torrentjedi.util;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.xml.sax.SAXException;

import com.sun.org.apache.xerces.internal.parsers.SAXParser;

import torrentjedi.core.Configuration;
import torrentjedi.core.RSSChannel;
import torrentjedi.core.SourceConfiguration;
import torrentjedi.core.TorrentSource;
import torrentjedi.data.ConfigurationHandler;
import torrentjedi.data.RSSHandler;

/**
 * @author Alper Kokmen
 *
 */
public class JEDIUtils {
	public static final String CONFIG_FILE = "conf/config.xml";
	
	public static final String SUCCESS = "success";
	public static final String VALIDATION_ERROR = "validation error";
	
	public static Configuration readConfig() {
		Configuration configuration = new Configuration();
		ConfigurationHandler cHandler = new ConfigurationHandler(configuration);
		
		SAXParser cParser = new SAXParser();
		cParser.setContentHandler(cHandler);
		
		try {
			cParser.parse(CONFIG_FILE);
			
			SourceConfiguration sourceConfiguration = new SourceConfiguration();
			ConfigurationHandler scHandler = new ConfigurationHandler(sourceConfiguration);
			
			SAXParser scParser = new SAXParser();
			scParser.setContentHandler(scHandler);
			
			scParser.parse(configuration.getSourceConfigString());
			configuration.setSourceConfiguration(sourceConfiguration);
			
//			System.out.println("DEBUG@TorrentJEDI:JEDIUtils readConfig configuration");
//			System.out.println(configuration);
		} catch (SAXException e) {
			System.out.println("ERROR@TorrentJEDI:JEDIUtils readConfig failed to parse configuration file");
			configuration = null;
		} catch (IOException e) {
			System.out.println("ERROR@TorrentJEDI:JEDIUtils readConfig failed to read configuration file");
			configuration = null;
		}
		
		return configuration;
	}
	
	public static List<RSSChannel> performAutoLoad(List<TorrentSource> torrentSources) {
		List<RSSChannel> channels = new ArrayList<RSSChannel>();
		
		for (TorrentSource torrentSource : torrentSources) {
			if (torrentSource.getAutoLoad()) {
				channels.add(performLoad(torrentSource));
			}
		}
		
		return channels;
	}
	
	public static RSSChannel performLoad(TorrentSource torrentSource) {
		RSSChannel channel = new RSSChannel();
		RSSHandler handler = new RSSHandler(channel);
		
		SAXParser parser = new SAXParser();
		parser.setContentHandler(handler);
		
		try {
			parser.parse(torrentSource.getUrl());
//			System.out.println("DEBUG@TorrentJEDI:JEDIUtils performLoad channel");
//			System.out.println(channel);
		} catch (SAXException e) {
			System.out.println("ERROR@TorrentJEDI:JEDIUtils performLoad failed to parse rss file");
			channel = null;
		} catch (IOException e) {
			System.out.println("ERROR@TorrentJEDI:JEDIUtils performLoad failed to read rss file");
			channel = null;
		}
		
		return channel;
	}
}
